<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category TestJob.php
 * @Depiction:
 * Author: hwy
 * Date: 2021/2/8  17:18
 * +--------------------------------------------------------+
 */

namespace App\Jobs;

use App\Services\DingService;
use App\Services\StockArticleService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class YbsArticleJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    // 用户类
    protected $data;

    /**
     * 任务可尝试的次数
     *
     * @var int
     */
    public $tries = 5;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $insert_data = $this->data;

        try {
            StockArticleService::getInstance()->addData($insert_data);
        } catch (\Exception $e) {
            Log::info('ysb_article add error');
            return true;
        }

        //发送通知
        foreach ($insert_data as $value) {
            $summary = $value['summary'];
            if (empty($summary)) {
                $summary = $value['content'];
            }
            $title = $value['title'];


            // 发送邮件
            $setting = sys_config();
            try {
                if (!empty($setting['ybs_emial']) && $setting['ybs_emial'] == 1) {
                    $email_arr = [$setting['site_email']];
                    $send      = [
                        'title'   => $title,
                        'summary' => $summary,
                    ];
                    $view      = 'emails.article';

                    $email_data['title']     = $title;
                    $email_data['summary']   = $summary;
                    $email_data['email_arr'] = $email_arr;
                    $email_data['send']      = $send;
                    $email_data['view']      = $view;
                    dispatch((new EmailsJob($email_data))->onQueue('default'));
                }
            } catch (\Exception $e) {
                Log::info('ysb_article send email error');
            }
            try {
                //发送钉钉
                if (!empty($setting['ybs_dd']) && $setting['ybs_dd'] == 1) {
                    DingService::getInstance()->textMsgSend($title . PHP_EOL . $summary, [], 2);
                }
            } catch (\Exception $e) {
                Log::info('ysb_article send dingding error');
            }
            try {
                //发送微信
                if (!empty($setting['ybs_weixin']) && $setting['ybs_weixin'] == 1) {
                    StockArticleService::getInstance()->returnWxArticle($title);
                }
            } catch (\Exception $e) {
                Log::info('ysb_article send weixin error');
            }


        }


        return true;

    }

}
